<?php
include 'portlet_header.php';
if (getParameter("operation")!=null) $operation=getParameter("operation");
if (getParameter("container")!=null) { $container=getParameter("container"); if ( $container=="") $container="container1";} else $container="container1";

if (getParameter("portlet")!=null) $portlet=getParameter("portlet");
if (getParameter("portletId")!=null) $portletId=getParameter("portletId");
if (getParameter("pageId")!=null) $pageId=getParameter("pageId");
//if (getParameter("porltetId")) $porltetId=getParameter("porltetId")!=null;
//if (getParameter("coupleId")) $coupleId=getParameter("coupleId")!=null;
if (getParameter("order")!=null) $order=getParameter("order");
if (getParameter("portalId")!=null) $portalId=getParameter("portalId");

if($operation=="savenewavance")
{
    $dossierId=getParameter("dossierId");
    $montant=getParameter("montant");
    insertObjByKey("cabinet_dossier_avance","dossierId","montant","portalId","date",$dossierId,$montant,$portalId,'NOW()');
    $calendar = R::dispense('jqcalendar');
    $calendar->Subject="testBR";
    $calendar->StartTime=ew_CurrentDateTime();
    $calendar->EndTime=ew_CurrentDateTime();
    R::store($calendar);
    echo "ok";


}elseif($operation=="savenewavancefournisseur")
{
    $fournisseurId=getParameter("fournisseurId");
    $montant=getParameter("montant");
    insertObjByKey("cabinet_fournisseur_avance","fournisseurId","montant","portalId","date",$fournisseurId,$montant,$portalId,'NOW()');
    echo "ok";


}elseif($operation=="getdents")
{
	
$dossierId=getParameter("dossierId");



$SQL="SELECT dentition  FROM cabinet_dossier WHERE id=$dossierId ";

//echo $SQL;

$rs=ew_LoadRecordset($SQL);


$GLOBALS["conn"]->BeginTrans();

$dents=array("11","12","13","14","15","16","17","18","21","22","23","24","25","26","27","28","31","32","33","34","35","36","37","38","41","42","43","44","45","46","47","48");
$arr_dent_temp=array("51","52","53","54","55","61","62","63","64","65","71","72","73","74","75","81","82","83","84","85");
if ($rs->fields["dentition"]=="Temporaire")  $dents=$arr_dent_temp;



if ($rs->fields["dentition"]=="Mixte")
{
	$dents=array_merge($dents,$arr_dent_temp);
}

echo "&dents=";
	foreach ($dents as $dent)
	{
		echo $dent.",";
		
	}
	
	
}elseif($operation=="getdentsstatus")
{
	
$dossierId=getParameter("dossierId");

$SQL="SELECT
cabinet_dent_default.dent,
cabinet_dent_default_etat.nom,
cabinet_dent_default_etat.color
FROM
cabinet_dent_default
INNER JOIN cabinet_dent_default_etat ON cabinet_dent_default.etatId = cabinet_dent_default_etat.id
WHERE
cabinet_dent_default.dossierId = $dossierId";



$rs=ew_LoadRecordset($SQL);
echo "&dents=";
while (!$rs->EOF){
	
	echo "".$rs->fields["dent"]."=".$rs->fields["nom"].",";
	$rs->MoveNext();
}

$rs=ew_LoadRecordset($SQL);
echo "&colorsdents=";
while (!$rs->EOF){

	echo "".$rs->fields["dent"]."=".preg_replace("/#/", "", $rs->fields["color"]).",";
	$rs->MoveNext();
}





	
	
	
}elseif($operation=="getnewnumdossier")
{
	//$dossierId=getParameter("dossierId");
	
	$datecreation=date("Y");
	$firstCaracter=substr(getParameter("nom"), 0,1);
	$SQL="SELECT count(*) as num FROM `cabinet_dossier` WHERE cabinet_dossier.portalId = $portalId AND YEAR(cabinet_dossier.dateDeCreation) = '$datecreation' AND cabinet_dossier.nom LIKE '".$firstCaracter."%'";
//echo $SQL;

	$datecreation=date("y");
	$rs=ew_LoadRecordset($SQL);

	echo $firstCaracter."/".$datecreation."/".($rs->fields["num"]+1);

	
	
	
}elseif($operation=="savenewacte")
{
	$newacteId=getParameter("newacteId");
	$soinId=getParameter("soinId");
	$dent=getObjByKey("cabinet_soins","dent","id",$soinId);
	$naturesoinId=getObjByKey("cabinet_soins","natureDeSoin","id",$soinId);
	$dossierId=getObjByKey("cabinet_soins","dossierId","id",$soinId);
insertObjByKey("cabinet_soins","date","dent","natureDeSoin","acte","dossierId","portalId","NOW()",$dent,$naturesoinId,$newacteId,$dossierId,$portalId);
	
	
echo "ok";
	
	//echo $rs->fields["id"];
	
	
}elseif($operation=="ajoutsalleattente")
{
	

insertObjByKey("cabinet_attente","dossierId","datearrive","portalId",getParameter("dossierId"),date("Y-m-d H:i:s"),$portalId);
	
	
echo "ok";
	
	//echo $rs->fields["id"];
	
	
}
elseif($operation=="soins")
{
	


}




elseif($operation=="checkmontant")
{
	
	$SQL="SELECT id
FROM
cabinet_soins
WHERE
cabinet_soins.dossierId = ".getParameter("dossierId")." AND
cabinet_soins.dent = ".getParameter("dent")." AND
cabinet_soins.natureDeSoin = ".getParameter("natureDeSoin")."";
	
	$rs=ew_LoadRecordset($SQL);
	
	if ($rs && $rs->RecordCount()>0) echo "true"; //hide
	else echo "false";
	
	//echo $rs->fields["id"];
	
	
}elseif($operation=="checkrendezvous")
{
	$max=$app_cabinet_maxrendezvous;
	$daterendezvous=getParameter("daterendezvous");
	$temps=getParameter("temps");

	$tempf="maxpm";
	if ($temps=="Matin") $tempf="maxam";
	$SQL="SELECT
cabinet_maxrendezvous.$tempf as max
FROM
cabinet_maxrendezvous
WHERE
portalId=$portalId and 
cabinet_maxrendezvous.datedebut <= '$daterendezvous' AND
cabinet_maxrendezvous.datefin >= '$daterendezvous'";
	
	//echo $SQL; 
	$rs=ew_LoadRecordset($SQL);
	
	if ($rs->RecordCount()>0) $max=$rs->fields["max"];
	
	
	$SQL="SELECT
count(*) as cnt
FROM 
cabinet_rendezvous
WHERE
cabinet_rendezvous.prochainrendezvous = '$daterendezvous' AND
cabinet_rendezvous.prochainrendezvous_temp = '$temps'";
//echo $SQL;
	$count=0;
$rs=ew_LoadRecordset($SQL);

if ($rs->RecordCount()>0) $count=$rs->fields["cnt"];

if ($count<$max)

	echo "true";
else 
	echo "false";
}
include 'portlet_footer.php';
?>